﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Background.Controllers
{
    using Data.Car_Rental;
    using Common.Background.Web.Controllers;
    using Entity.Car_Rental;
    using Entity.CommonEnums;
    public class OrderController : BackgroundBaseController
    {
        IOrderRespository iOrderRespository = null;
        IOrderItemRespository iOrderItemRespository = null;
        IRentalRecordRepository iRentalRecordRepository = null;
        public OrderController()
        {
            iOrderRespository = new OrderRespository();
            iOrderItemRespository = new OrderItemRespository();
            iRentalRecordRepository = new RentalRecordRepository();
        }
        //
        // GET: /Order/

        public ActionResult Index(int? page)
        {
            VP = new Entity.VPredication();
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            var model = iOrderRespository.GetOrder(VP, PP);
            return View(model);
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id)
        {
            var model = iOrderRespository.GetDetailModel().FirstOrDefault(i => i.OrderID == id);
            return View(model);
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Success(int id)
        {
            Order entity = new Order
            {
                OrderID = id,
                UpdateDate = DateTime.Now,
                OrderStatus = (int)OrderStatus.Finish,
            };
            iOrderRespository.FinishOrder(entity);
            return AlertToUrl("Index");
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        public ActionResult FinishRental(int id)
        {
            iRentalRecordRepository.FinishRentalCar(iRentalRecordRepository.Find(id));
            return AlertToUrl("Index");
        }
    }
}
